package com.zzptc.myk.a1028;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ListView listView;
    List<Book> list;
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView=findViewById(R.id.lv_book);
        list =new ArrayList<>();
        Book a = new Book(R.mipmap.a,1,"侦查与小偷",25);
        Book b = new Book(R.mipmap.b,2,"漫画万物由来",35);
        Book c = new Book(R.mipmap.c,3,"小钱狗狗",29);
        Book d = new Book(R.mipmap.d,4,"没头脑和不高兴",49);
        list.add(a);
        list.add(b);
        list.add(c);
        list.add(d);
        for(Book s:list){
            Log.d(TAG,s.getName());
        }
        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);

    }
    class  MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Book book = list.get(position);
            View inflate = View.inflate(MainActivity.this, R.layout.item_book, null);
            ImageView iv_pic = inflate.findViewById(R.id.iv_pic);
            iv_pic.setImageResource(book.getPic());
            TextView tv_id=inflate.findViewById(R.id.tv_id);
            tv_id.setText(book.getId()+"");//要将整数转换成字符串
            TextView  tv_name= inflate.findViewById(R.id.tv_name);
            tv_name.setText(book.getName());
            TextView tv_date=inflate.findViewById(R.id.tv_price);
            tv_date.setText(book.getPrice()+"");
            return inflate;

        }
    }
}
